package util;

import java.util.Iterator;
import java.util.List;

import jiyici.Constant.ModelColumn;
import models.Category;
import models.Item;
import models.ItemParam;
import models.Record;
import models.RecordParam;
import models.ServerBaseModel;
import models.User;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.node.ArrayNode;
import org.codehaus.jackson.node.ObjectNode;

import play.libs.Json;

public class ModelUtil {
    
    public static ServerBaseModel createModel(String clazzName) {
        if (Category.class.getSimpleName().equals(clazzName))
            return new Category();
        else if (Item.class.getSimpleName().equals(clazzName))
            return new Item();
        else if (ItemParam.class.getSimpleName().equals(clazzName))
            return new ItemParam();
        else if (Record.class.getSimpleName().equals(clazzName)) {
            return new Record();
        } else if (RecordParam.class.getSimpleName().equals(clazzName)) {
            return new RecordParam();
        }
        return null;
    }

    /**
     * Only for register and login. For other query should not send "token".
     * 
     * @param user
     * @return
     */
    public static JsonNode jsonOf(User user) {
        ObjectNode node = Json.newObject();
        node.put(ModelColumn.USER_COLUMN_ID, user.id);
        node.put(ModelColumn.USER_COLUMN_EMAIL, user.email);
        node.put(ModelColumn.USER_COLUMN_USERNAME, user.userName);
        node.put(ModelColumn.USER_COLUMN_TOKEN, user.token);
        return node;
    }

    public static JsonNode jsonOf(Item item) {
        ObjectNode node = Json.newObject();
        node.put(ModelColumn.BASE_COLUMN_ID, item.getId());
        node.put(ModelColumn.BASE_COLUMN_LASTMODIFIED, item.lastmodified);
        node.put(ModelColumn.BASE_COLUMN_DELETED, item.deleted);
        node.put(ModelColumn.ITEM_COLUMN_NAME, item.itemName);
        node.put(ModelColumn.ITEM_COLUMN_CATEGORYID, item.categoryId);
        node.put(ModelColumn.ITEM_COLUMN_DISPLAY_INDEX, item.displayIndex);
        node.put(ModelColumn.ITEM_COLUMN_RECORD_COUNT, item.recordCount);
//        ArrayNode arrayNode = node.arrayNode();
//        for (Iterator<ItemParam> i = item.params.iterator(); i.hasNext();) {
//            ItemParam param = i.next();
//            arrayNode.add(jsonOf(param));
//        }
//        node.put(ModelColumn.ITEM_COLUMN_PARAMS, arrayNode);
        return node;
    }

    public static JsonNode jsonOfItemList(List<Item> itemList) {
        ObjectNode node = Json.newObject();
        ArrayNode arrayNode = node.arrayNode();
        if (itemList != null) {
            for (Iterator<Item> i = itemList.iterator(); i.hasNext();) {
                Item item = i.next();
                arrayNode.add(jsonOf(item));
            }
        }
        node.put("items", arrayNode);
        return node;
    }
    
    public static JsonNode jsonOf(Category category) {
        ObjectNode node = Json.newObject();
        node.put(ModelColumn.BASE_COLUMN_ID, category.getId());
        node.put(ModelColumn.BASE_COLUMN_LASTMODIFIED, category.lastmodified);
        node.put(ModelColumn.BASE_COLUMN_DELETED, category.deleted);
        node.put(ModelColumn.CATEGORY_COLUMN_NAME, category.name);
        node.put(ModelColumn.CATEGORY_COLUMN_DISPLAY_ORDER, category.displayOrder);
        return node;
    }
    
    public static JsonNode jsonOfCategoryList(List<Category> categories) {
        ObjectNode node = Json.newObject();
        ArrayNode arrayNode = node.arrayNode();
        if (categories != null) {
            for (Iterator<Category> i = categories.iterator(); i.hasNext();) {
                Category category = i.next();
                arrayNode.add(jsonOf(category));
            }
        }
        node.put("categories", arrayNode);
        return node;
    }
    
    

    public static JsonNode jsonOf(ItemParam itemParam) {
        ObjectNode node = Json.newObject();
        node.put(ModelColumn.BASE_COLUMN_ID, itemParam.getId());
        node.put(ModelColumn.BASE_COLUMN_LASTMODIFIED, itemParam.lastmodified);
        node.put(ModelColumn.BASE_COLUMN_DELETED, itemParam.deleted);
        node.put(ModelColumn.ITEM_PARAM_COLUMN_NAME, itemParam.itemParamName);
        node.put(ModelColumn.ITEM_PARAM_COLUMN_TYPE, itemParam.paramType);
        node.put(ModelColumn.ITEM_PARAM_COLUMN_IS_MUST, itemParam.isMust);
        node.put(ModelColumn.ITEM_PARAM_COLUMN_DISPLAY_ORDER, itemParam.displayOrder);
        return node;
    }

    public static JsonNode jsonOf(Record record) {
        ObjectNode node = Json.newObject();
        node.put(ModelColumn.BASE_COLUMN_ID, record.getId());
        node.put(ModelColumn.BASE_COLUMN_LASTMODIFIED, record.lastmodified);
        node.put(ModelColumn.BASE_COLUMN_DELETED, record.deleted);
        node.put(ModelColumn.RECORD_ITEM_ID, record.itemId);
        node.put(ModelColumn.RECORD_RECORD_TIME, record.recordTime);
//        ArrayNode arrayNode = node.arrayNode();
//        for (Iterator<RecordParam> i = record.params.iterator(); i.hasNext();) {
//            RecordParam param = i.next();
//            arrayNode.add(jsonOf(param));
//        }
//        node.put(ModelColumn.RECORD_PARAMS, arrayNode);
        return node;
    }

    public static JsonNode jsonOf(RecordParam recordParam) {
        ObjectNode node = Json.newObject();
        node.put(ModelColumn.BASE_COLUMN_ID, recordParam.getId());
        node.put(ModelColumn.BASE_COLUMN_LASTMODIFIED, recordParam.lastmodified);
        node.put(ModelColumn.BASE_COLUMN_DELETED, recordParam.deleted);
        node.put(ModelColumn.RECORD_PARAM_ITEM_PARAM_ID, recordParam.itemParamId);
        node.put(ModelColumn.RECORD_PARAM_VALUE, recordParam.value);
        return node;
    }
}
